from functools import wraps

from flask import session, g, jsonify
from werkzeug.utils import redirect


def pc_login_required(func):
    """
    PC登陆检测
    :param args_:
    :param kwargs_:
    :return:
    """

    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        print('登录检查')
        print(user_id)
        if user_id is not None:
            g.user_id = user_id
            return func(*args, **kwargs)
        else:
            return redirect('/login')

    return wrapper


def api_login_required(*args_, **kwargs_):
    """
    登陆检测
    :param args_:
    :param kwargs_:
    :return:
    """

    def wrapper(func):
        def __decorated_view(*args, **kwargs):
            user_id = session.get('user_id')
            if user_id is not None:
                g.user_id = user_id
                return func(*args, **kwargs)
            else:
                return jsonify(errno='403', errmsg='用户未登录')

        return __decorated_view

    return wrapper


def pc_admin_login_required(func):
    """
    PC后台登陆检测
    :param args_:
    :param kwargs_:
    :return:
    """

    def wrapper(*args, **kwargs):
        admin_user_id = session.get('admin_user_id')
        if admin_user_id is not None:
            g.admin_user_id = admin_user_id
            return func(*args, **kwargs)
        else:
            return redirect('/admin/login')

    return wrapper


def api_admin_login_required(*args_, **kwargs_):
    """
    后台登陆检测
    :param args_:
    :param kwargs_:
    :return:
    """

    def wrapper(func):
        def decorated_view(*args, **kwargs):
            admin_user_id = session.get('admin_user_id')
            if admin_user_id is not None:
                g.admin_user_id = admin_user_id
                return func(*args, **kwargs)
            else:
                return jsonify(errno='403', errmsg='用户未登录')

        return decorated_view

    return wrapper
